package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;

/**
 * 封装Response对象，需要依赖OutPutStream
 * 该对象需要提供核心方法，输出Html
 * @program: minicat
 * @description
 * @author: luffy
 * @create: 2020-08-02 21:28
 */
public class Response {
    private OutputStream outputStream;

    public Response() {

    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }
    /**
     * 根据url来获取到静态资源的绝对路径，进一步根据绝对路径读取改静态资源文件，最终通过输出流输出
     * @param path
     */
    public void outPutHtml(String path) throws IOException, URISyntaxException {
        //获取静态资源文件的绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);

        //输入静态资源文件
        File file = new File(absoluteResourcePath);
        if (file.exists()&&file.isFile()) {
            //输入静态资源
            StaticResourceUtil.outPutStaticResource(new FileInputStream(file),outputStream);
        }else {
            //输入404
            output(HttpProtocolUtil.getHttpHeader404());

        }
    }
}
